
from django.conf import settings
from django_filters import *
from django_filters.rest_framework import DjangoFilterBackend, FilterSet
from rest_framework.filters import SearchFilter, OrderingFilter

from mysqldb.models import *

class ListDjangoFilterBackend(DjangoFilterBackend):
    """自己定义list过滤器"""

    def get_schema_fields(self, view):
        if view.action not in ["list"]:
            return []
        return super().get_schema_fields(view)
    
class AllinpayAuthViewsFilterSet(FilterSet):
    """自定义过滤器""" 
    staff = NumberFilter(field_name="staff", method="filter_factor",help_text="因数",label="类型因数")
    class Meta:
        model = AllinpayAuthViews
        fields = [
            "staff"
        ]  
        
    def has_Chmod(self, staff):
        """返回包含指定因子的数列"""
        return [i for i in range(settings.PLACE_CHMOD_LIST.__len__()) if staff in settings.PLACE_CHMOD_LIST[i]]        

    def filter_factor(self, qs, name, value):
        lookup = "__".join([name, "in"])
        return qs.filter(**{lookup: self.has_Chmod(value)})   
   
